package com.lwc.cfdns.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.Timestamp;

/**
 * mybatis-plus相关
 *
 * @author luwc
 * @title MybatisPlusConfig
 * @description
 * @time 2023/4/25 11:24:17
 * @version: 1.0
 */
@Component
@EnableTransactionManagement // 开启事务
@Configuration
public class MybatisPlusConfig implements MetaObjectHandler {
    public static final String CREATED_FIELD = "createdAt";
    public static final String CREATOR_ID_FIELD = "creatorId";

    public static final String MODIFIED_TIME_FIELD = "updatedAt";
    public static final String UPDATER_ID_FIELD = "updaterId";

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 注册乐观锁插件
//        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
//        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        return interceptor;
    }

    /**
     * 自动填充
     * https://zhuanlan.zhihu.com/p/610126081
     *
     * @param metaObject
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/4/25 11:29:40
     */
    @Override
    public void insertFill(MetaObject metaObject) {
//        this.setFieldValByName("createTime",new Date(),metaObject); new Timestamp(System.currentTimeMillis()) 格式化后的时间格式
        if (metaObject.hasSetter(CREATED_FIELD)) {
            this.setFieldValByName(CREATED_FIELD, new Timestamp(System.currentTimeMillis()), metaObject);//时间戳
            this.setFieldValByName(MODIFIED_TIME_FIELD, new Timestamp(System.currentTimeMillis()), metaObject);//时间戳
        }
        if (metaObject.hasSetter(CREATOR_ID_FIELD)) {
            this.strictInsertFill(metaObject, CREATOR_ID_FIELD, Long.class, getUserIdSafely());//创建者
        }
    }

    /**
     * 自动填充
     *
     * @param metaObject
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/4/25 11:29:54
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter(MODIFIED_TIME_FIELD)) {
            this.setFieldValByName(MODIFIED_TIME_FIELD, new Timestamp(System.currentTimeMillis()), metaObject);//时间戳
        }
        if (metaObject.hasSetter(UPDATER_ID_FIELD)) {
            this.strictUpdateFill(metaObject, UPDATER_ID_FIELD, Long.class, getUserIdSafely());// 修改者
        }
    }

    /**
     * 操作者
     *
     * @param
     * @return Long
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/4/26 14:23:50
     */
    public Long getUserIdSafely() {
        Long userId = 1L;
        try {
            // todo 获取操作者
        } catch (Exception e) {
            System.out.println("can not find user in current thread.");
        }
        return userId;
    }

}
